<?php
/**
 * Creater: 李青林
 * Date Time : 2018/8/14 9:52
 */

namespace App\Http\Models;

use Auth;

class UserCapital extends BaseModel
{
    protected $table = 'users_capital';
    protected $guarded = [];

    /**
     * @param $userId 用户id
     * @param $number 点劵数量
     * @param $type 类型,1商品退回 ,0 发布商品消费 ，2充值 (注: on/off 是后台异步编辑使用)
     * @param $content 点券消费记录内容
     * @return mixed
     */
    public static function addUserCapital($userId, $number, $type, $content = '', $goods_id = 0, $refuse = '')
    {
        $model = self::where(['user_id' => $userId])->first();
        if ($model) {
            if ($type == 1 || $type == 2) {
                $model->increment('voucher_number', $number);
            } else {
                if ($model->voucher_number < $number) {
                    $model->voucher_number = 0;
                } else {
                    $model->decrement('voucher_number', $number);
                }
            }
            $bool = $model->save();
        } else {
            $bool = self::create([
                'voucher_number' => $number,
                'user_id' => $userId,
            ]);
        }
        //点券消费记录日志
        $log_bool = VoucherLog::addUserVoucherLog($userId, $content, $number, $type, $goods_id, $refuse);
        //点券月统计
        $user_static = StatisticsUserVoucher::updateUserVoucher($userId, $number, $type, $goods_id);
        return ($bool && $log_bool && $user_static);
    }

    /**
     * @param $userId 用户id
     * @param $capital 积分数量
     * @param $content_info 扣分详情
     * @param $type 类型,1 增加,0其他扣除积分   (注: on/off 是后台异步编辑使用)
     * @param $goodsId 商品id
     * @return mixed
     */
    public static function addUserIntegral($userId, $integral, $type, $content_info = '', $goodsId, $refuse)
    {
        $model = self::where(['user_id' => $userId])->first();
        if ($model) {
            if ($type == 1) {
                //处理非1号管理员处理增加用户积分的情况
                if ($model->integral == 0 && date('d') != 1 && date('H') != 0) {
                    StatisticsUser::addZeroInergralUser(1);
                }
                $model->increment('integral', $integral);
            } elseif ($type == 2) {
                $model->integral = env('UserIntegral');
            } else {
                if ($model->integral <= $integral) {
                    $model->integral = 0;
                    StatisticsUser::addZeroInergralUser();//更新0积分用户
                } else {
                    $model->decrement('integral', $integral);
                }
            }
            $bool = $model->save();
        } else {
            $bool = self::create([
                'integral' => env('UserIntegral'),
                'user_id' => $userId
            ]);
        }
        //积分消费记录
        $log_bool = UserIntegralLog::addUserIntegralLog($userId, $content_info, $integral, $goodsId, $type, $refuse);
        return ($log_bool && $bool);
    }

    /**
     * 判断用户是否可以发单
     * @param $goods_type //0平推，1爆款,2 秒杀
     */
    public static function IsSendGoods($goods_type)
    {
        $UserCapital = self::where(['user_id' => Auth::id()])->first();
        $is_send = false;
        if ($goods_type == 2) {
            $is_send = $UserCapital->voucher_number >= env('Voucher_Kill_Deduct');
        } elseif ($goods_type == 1) {
            $is_send = $UserCapital->voucher_number >= env('Voucher_Fire_Deduct');
        } elseif ($goods_type == 0) {
            $is_send = $UserCapital->voucher_number >= env('Voucher_Normal_Deduct');
        }
        return boolval($is_send && $UserCapital->integral > 0);
    }

    /**
     * 获取用户的点券以及积分信息
     * @param $userId
     * @return mixed
     */
    public static function getUserCapital($userId)
    {
        return self::select('voucher_number', 'integral')->where(['user_id' => $userId])->first();
    }

    public function user()
    {
        return $this->belongsTo('App\Http\Models\User', 'id', 'userid');
    }

    public static function test()
    {

    }
}